import {
  postForm
}
from '../../../utils/request'
Page({

  data: {
    activeTabIndex: 0,
    pageData: [{
      pageNo: 1,
      hasNextPage: true,
      orderList: []
    }, {
      pageNo: 1,
      hasNextPage: true,
      orderList: []
    }, {
      pageNo: 1,
      hasNextPage: true,
      orderList: []
    }]
  },
  onLoad(options) {},
  onReady() {},
  onShow() {
    this.loadOrder();
  },
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {
    console.info('reach bottom....')
    this.loadOrder();
  },
  onShareAppMessage() {},
  onTabsChange(e) {
    this.setData({
      activeTabIndex: e.detail.value | 0,
    })
    this.loadOrder();
  },
  loadOrder() {
    if (!this.data.pageData[this.data.activeTabIndex].hasNextPage) {
      return;
    }
    let url = 'front/api/trade/list';
    let statusText = ['ALL', 'PENDING', 'FINISHED']
    let pageQuery = this.data.pageData[this.data.activeTabIndex];
    let param = {
      pageNo: pageQuery.pageNo,
      size: 6,
      status: statusText[this.data.activeTabIndex]
    };

    postForm({
      url: url,
      data: param
    }).then(resp => {
      let pageData = this.data.pageData;
      resp.content.forEach(item => {
        item['orderStatusDisplay'] = this.getOrderStatusDisplay(item.trade.payStatus, item.trade.serviceStatus);
      })
      let content = pageData[this.data.activeTabIndex];
      content['orderList'] = [...content.orderList, ...resp.content];
      content['hasNextPage'] = !resp.last;
      content['pageNo']++;
      this.setData({
        pageData: pageData
      })
    })
  },
  getOrderStatusDisplay(payStatus, serviceStatus) {
    switch (payStatus) {
      case 'WAIT':
        return '待支付';
      case 'SUCCESS':
        switch (serviceStatus) {
          case 'WAIT':
          case 'PENDING':
            return '服务中';
          case 'FINISHED':
            return '已完成';
          case 'BROKE':
            return '服务中止';
        }
        break;
      default:
        return '订单取消'
    }
  }
})